package com.demo01_video.uitl;

import com.demo01_video.config.MinioConfig;
import io.minio.MinioClient;
import io.minio.GetObjectArgs;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.UUID;

// MinIO工具类（上传）
@Component
@Slf4j
public class MinioUtil {

    @Autowired
    private MinioClient minioClient;

    @Value("${minio.bucket}")
    private String bucketName;

    public String upload(MultipartFile  file) throws Exception{
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID() + "." + getFileExtension(originalFilename);

        log.info("开始上传文件到MinIO，原始文件名: {}，新文件名: {}", originalFilename, fileName);
        
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(fileName)
                        .stream(file.getInputStream(), file.getSize(), -1)
                        .contentType(file.getContentType())
                        .build()
        );
        
        String url = "http://localhost:9000/"+bucketName+"/"+fileName;
        log.info("文件上传成功，URL: {}", url);
        return url;
    }

    private String getFileExtension(String filename) {
        if (filename == null || filename.lastIndexOf(".") == -1) {
            return "";
        }
        return filename.substring(filename.lastIndexOf(".") + 1);
    }
    
    // 下载文件到本地
    public File downloadFile(String objectName) throws Exception {
        log.info("开始从MinIO下载文件: {}", objectName);
        InputStream inputStream = minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .build()
        );
        
        // 创建临时文件
        File tempFile = File.createTempFile("minio-", ".tmp");
        Files.copy(inputStream, tempFile.toPath());
        inputStream.close();
        log.info("文件下载成功，本地路径: {}", tempFile.getAbsolutePath());
        return tempFile;
    }
    
    // 上传本地文件
    public String uploadFile(File file, String objectName) throws Exception {
        log.info("开始上传本地文件到MinIO，文件路径: {}，对象名: {}", file.getAbsolutePath(), objectName);
        minioClient.putObject(
                PutObjectArgs.builder()
                        .bucket(bucketName)
                        .object(objectName)
                        .stream(Files.newInputStream(file.toPath()), file.length(), -1)
                        .build()
        );
        String url = "http://localhost:9000/"+bucketName+"/"+objectName;
        log.info("本地文件上传成功，URL: {}", url);
        return url;
    }
    
    // 获取bucket名称的公共方法
    public String getBucketName() {
        return bucketName;
    }
}